package com.zb.api.business.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSONObject;
import com.zb.api.business.entity.UserEntity;
import com.zb.api.business.service.UserService;
import com.zb.api.common.conts.Const;
import com.zb.api.common.models.Result;
import com.zb.api.common.models.ServerResponse;
import com.zb.api.utils.JwtUtils;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/currency")
public class CurrencyController {

    private final static String FILE_BASE_PATH = "/data/img";

    @RequestMapping("/fileUpload")
    public Result fileUpload(HttpServletRequest request){
        boolean isMultipart = ServletFileUpload.isMultipartContent(request);
        System.out.println("是否有上传文件:"+isMultipart);
        if (isMultipart == false) {
            return new Result("表单中不含文件");
        }
        /*List<MultipartFile> list = ((MultipartHttpServletRequest)request).getFiles("files");
        if (list.isEmpty()) {
            return new Result("文件不存在");
        }
        List<String> fileList = new ArrayList<>();
        for (MultipartFile multipartFile : list) {
            if (list.isEmpty()) {
                continue;
            }

            // 文件名
            String fileName = multipartFile.getOriginalFilename();
            System.out.println("文件名： " + fileName);
            // file size:
            Long length = multipartFile.getSize();
            System.out.println("files size :"+length);

            // 文件后缀
            int lastDot = fileName.lastIndexOf(".");
            lastDot++;
            String fileType = fileName.substring(lastDot);
            // 重新生成唯一文件名，用于存储数据库
            String fileSn = UUID.randomUUID().toString();
            Map<String, String> map2 = new HashMap<String, String>();
            String destFilePath = FILE_BASE_PATH+"/"+fileSn+"."+fileType;
            File dest = new File(destFilePath);
            try {
                multipartFile.transferTo(dest.getAbsoluteFile());
            } catch (IOException e) {
                System.out.println("save ioexception");
                e.printStackTrace();
                return new Result("上传失败", "1");
            }
            fileList.add(destFilePath);
        }*/

        MultipartFile multipartFile = ((MultipartHttpServletRequest)request).getFile("files");
        if (multipartFile==null) {
            return new Result("文件不存在");
        }
        // 文件名
        String fileName = multipartFile.getOriginalFilename();
        System.out.println("文件名： " + fileName);
        // file size:
        Long length = multipartFile.getSize();
        System.out.println("files size :"+length);

        // 文件后缀
        int lastDot = fileName.lastIndexOf(".");
        lastDot++;
        String fileType = fileName.substring(lastDot);
        // 重新生成唯一文件名，用于存储数据库
        String fileSn = UUID.randomUUID().toString();
        Map<String, String> map2 = new HashMap<String, String>();
        String destFilePath = FILE_BASE_PATH+"/"+fileSn+"."+fileType;
        File dest = new File(destFilePath);
        try {
            multipartFile.transferTo(dest.getAbsoluteFile());
        } catch (IOException e) {
            System.out.println("save ioexception");
            e.printStackTrace();
            return new Result("上传失败");
        }
        return new Result("操作成功", destFilePath);
    }




}
